// 
// ToolbarButtonNode.cs
//  
// Author:
//       Tomas Vondracek <tomas.vondracek@gmail.com>
// 
// Copyright (c) 2010 Tomas Vondracek
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Windows.Forms;
using Mono.Addins;
using Common.Interfaces;
using System.Diagnostics;

namespace Common.Gui.GuiWinFormsLib.ExtensionNodes
{

	[ExtensionNode("ToolButton")]
	public class ToolBarButtonNode : ToolBarNode
	{
		[NodeAttribute("icon", false)]
		string icon;

		[NodeAttribute("label", false)]
		string label;

		[NodeAttribute("commandHandlerType",typeof(CommandHandler), true, "CommandHandler class")]
		string commandHandlerType;
		
		public override ToolStripItem GetToolItem()
		{
			ToolStripButton btn = null;
			
			if (!String.IsNullOrEmpty(this.commandHandlerType))
			{
				try
				{
					CommandHandler cmdHandler = (CommandHandler)Addin.CreateInstance(commandHandlerType);
					cmdHandler.CommandUpdate += HandleCommandHandlerUpdate;
					cmdHandler.Id = this.Id + "ToolButton";
					
					var cmdInfo = cmdHandler.CommandInfo;
					
					btn = new System.Windows.Forms.ToolStripButton(cmdInfo.Text);
					btn.Click += new EventHandler(btn_Click);
					btn.Enabled = cmdInfo.IsEnabled;
					btn.Visible = cmdInfo.IsVisible;
					
					// command info has no text to display
					if (!string.IsNullOrEmpty(this.label) && string.IsNullOrEmpty(cmdInfo.Text))
					{
						cmdInfo.Text = this.label;
						cmdInfo.IsEnabled = true;
						cmdInfo.IsVisible = true;
					}
					
					CommandManager.GetInstance().RegisterCommandHandler(cmdHandler, btn);
					cmdHandler.Update(cmdInfo);
				}
				catch (Exception ex) 
				{
					Logging.Logger.AddException(ex);	
				}
			}
			else
				Logging.Logger.AddWarning("No command handler specified for addin " + this.Id);
			
			return btn;
		}

		void HandleCommandHandlerUpdate (object sender, CommandUpdatedEventArgs e)
		{
			Debug.Assert(sender is CommandHandler);
			
			try
			{
				CommandManager.GetInstance().UpdateControlItem(e.Id, e.CommandInfo);
			} 
			catch (Exception ex) 
			{
				Logging.Logger.AddException(ex);	
			}
		}
		
		private void btn_Click(object sender, EventArgs e)
		{
			try
			{
				if (!String.IsNullOrEmpty(this.commandHandlerType))
				{
					CommandHandler commandHandler = (CommandHandler)Addin.CreateInstance(commandHandlerType);
					if (commandHandler != null)
					{
						commandHandler.Run();
					}
					else
					{
						Logging.Logger.AddError("The command " + commandHandlerType + " not found!");
					}
				}
				else
				{
					Logging.Logger.AddError("The command " + commandHandlerType + " is empty!");
				}
			}
			catch (Exception ex)
			{
				Logging.Logger.AddException(ex);
				MessageBox.Show(commandHandlerType + ": command exception. \n" + ex.ToString(), "Command exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}
		
	}
}
